<template>
  <!-- 登录页面大容器 -->
  <div class="login_container">
    <!-- 登录区域 登录盒子 -->
    <div class="login_box">
      <!-- logo区 -->
      <div class="avatar_box">
        <img src="../assets/img/logo.png" alt="" />
      </div>
      <!-- 登录表单 -->
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="login_from">
        <!-- 登录账户 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="用户名"></el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-lock" placeholder="密码"
            autocomplete="off"></el-input>
        </el-form-item>
        <!-- 提交按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/login.js";
export default {
  data() {
    return {
      // 提交表单对象
      loginForm: {
        username: 'admin',
        password: '123'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 15, message: '密码长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }

    }
  },
  // 函数
  methods: {
    // submitForm(formName) {
    //   this.$router.push("/index");
    // },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.loginForm).then(res => {
            
            if (res.data.message === "成功") {
              sessionStorage.setItem("token", res.data.data.accessToken);
              // console.log(res.data.data.employee.username);
              sessionStorage.setItem("username", res.data.data.employee.username);
              this.$router.push("/index");
            } else {
              this.$message.error(res.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style>
.login_container {
  background: linear-gradient(to top, #e7e7e7 0%, #e7e7e7 50%, #009688 50%, #009688 100%);
  /* background-color: #009688; */
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #efef;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login_from {
  position: absolute;
  right: 10%;
  bottom: 0px;
  width: 100%;
  padding: 0px 20px;
  box-sizing: border-box;
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.avatar_box img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.btns {
  display: flex;
  justify-content: space-evenly;
}
</style>